package com.cah.game;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cah.library.UserFunctions;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class GameNotCzarFragment extends GameUIFragment
{
	private String czarHandle;
	private ArrayList<Spinner> spinnerArray;
	private boolean userSubmitted;
	LinearLayout spinnerLayout;
	SparseArray<String> whiteIdTextMap;
	Button submitWhiteButton;
	
	public GameNotCzarFragment(String gameKey, JSONObject gameInfo)
	{
		super();
		this.gameKey = gameKey;
		this.gameInfo = gameInfo;
	}
	
	public GameNotCzarFragment()
	{
		super();
	}
	
	public void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
	}
	
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		
		
		spinnerArray = new ArrayList<Spinner>();
		
		
		try
		{
			czarHandle = gameInfo.getString("czarHandle");
			userSubmitted = gameInfo.getBoolean("userSubmitted");
			
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		
		
		
		gameHeader.setText("Round " + Integer.toString(currentRound) + ": " + czarHandle + " is picking.");
		spinnerLayout = (LinearLayout)main.findViewById(R.id.handSpinnerLinearLayout);
		
		submitWhiteButton = (Button)main.findViewById(R.id.submitWhiteButton);
		
			
		new HandIdsProcess().execute();
		
		submitWhiteButton.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View arg0)
			{
				ArrayList<Integer> whiteTexts = new ArrayList<Integer>();
				boolean copy = false;
				
				for(int i=0;i<pick;i++)
				{
					Integer nextPick = (Integer)(whiteIdTextMap.keyAt(whiteIdTextMap.indexOfValue(spinnerArray.get(i).getSelectedItem().toString())));
					
					if(whiteTexts.contains(nextPick))
					{
						copy = true;
					}
					whiteTexts.add(nextPick);
				}
				if(!copy)
				{
					int[] a = new int[whiteTexts.size()];
					for(int i=0;i<pick;i++)
					{
						a[i] = whiteTexts.get(i).intValue();
					}
					new SubmitWhiteProcess().execute(a);
				}
				else
				{
					Toast.makeText(main, "You can't submit the same card more than once.", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
			
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.game_not_czar, container, false);
	}
	
	private class HandIdsProcess extends AsyncTask<Void, Void, JSONArray>
	{

		@Override
		protected JSONArray doInBackground(Void... params)
		{
			try
			{
				return gameInfo.getJSONArray("hand");
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			
			return null;
		}
		
		protected void onPostExecute(JSONArray value)
		{
			try
			{
					int[] a = new int[value.length()];
					
					for(int i=0;i<value.length();i++)
					{
						a[i]=value.getInt(i);
					}
					
					try
					{
						GetWhiteCardTextProcess whiteProcess = new GetWhiteCardTextProcess();
						whiteProcess.execute(a);
						
						whiteIdTextMap = whiteProcess.get();
						
						instructionTextView.setText("Pick " + Integer.toString(pick));
						
						
						if(userSubmitted)
						{
							pick=1;
							instructionTextView.setText("You have already submitted.  Don't be greedy.");
							submitWhiteButton.setEnabled(false);
						}
						
						ArrayList<String> whiteTexts = new ArrayList<String>();
						
						for(int i=0;i<a.length;i++)
						{
							whiteTexts.add(whiteIdTextMap.get(a[i]));
						}
												
						for(int i=0;i<pick;i++)
						{
							Spinner spinner = new Spinner(main);
							ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(main, R.layout.spinner_item, whiteTexts);
							spinner.setAdapter(spinnerArrayAdapter);
							spinnerArray.add(spinner);
							spinnerLayout.addView(spinner);
						}
						
						
					} catch (InterruptedException e)
					{
						e.printStackTrace();
					} catch (ExecutionException e)
					{
						e.printStackTrace();
					}
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
		}
		
	}
	
	
	private class SubmitWhiteProcess extends AsyncTask<int[], Void, JSONObject>
	{
		ProgressDialog pdia;
		@Override
		protected void onPreExecute()
		{
			pdia = new ProgressDialog(main);
			pdia.setMessage("Submitting...");
			pdia.show();
		}
		
		@Override
		protected JSONObject doInBackground(int[]... params)
		{
			int[] ids = params[0];
			UserFunctions uf = new UserFunctions(userKey);
			return uf.submitWhite(ids, gameKey);
		}
		
		protected void onPostExecute(JSONObject json)
		{
			try
			{
				if(json.getInt("success")==1)
				{
					main.startGameFragment(gameKey, 1);
				}
				else
				{
					Toast.makeText(main, "Something went wrong!", Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			pdia.dismiss();
		}

	}
}
